function isEmpty(value) {
    if (typeof value === "undefined" || value === null) {
        return true;
    }

    if (typeof value === "string") {
        return value === "";
    }

    if (value instanceof Array) {
        return value.length === 0;
    }

    return false;
}

/**
 * 功能: 判断值是否无效
 */
function isNotEmpty(value) {
    return !isEmpty(value);
}

function mappingHandle(fillDate, mappings) {
    if (isEmpty(fillDate) || isEmpty(mappings)) {
        throw new Error("传入的数据异常, 无法转换");
    }

    var result = [];

    for (var i = 0; i < mappings.length; i++) {
        var mapping = mappings[i];
        var tempNode = {};

        for (var insuKey in mapping) {
            var mappingNode = mapping[insuKey];
            if (isEmpty(mappingNode)) {
                continue;
            }

            var mappingValue = "";

            if (typeof mappingNode === "string") {
                // 如果配置的是string, 就直接获取
                mappingValue = fillDate[mappingNode];
            } else if (typeof mappingNode === "object") {
                // 获取转换属性
                var mappingKey = mappingNode["mappingKey"];
                var mappingFunc = mappingNode["mappingFunc"];
                var require = mappingNode["require"];

                // 获取所配置的mapping对应的his中的值
                mappingValue = fillDate[mappingKey];

                var mappingValueBackup = mappingValue;
                // 如果配置的转换函数不为空, 就进行调用
                if (typeof mappingFunc === "function") {
                    try {
                        mappingValue = mappingFunc.call(null, mappingValue);
                    } catch (e) {
                        mappingValue = mappingValueBackup;
                    }
                }

                // 判断结果是否为必须参数
                if (require && isEmpty(mappingValue)) {
                    throw new Error("[" + mappingKey + "]的值不能为空!");
                }
            }

            tempNode[insuKey] = mappingValue;
        }

        result.push(tempNode);
    }

    return result;
}

var data = {
    id1: 1,
    name1: "s1",
    time1: "2019-08-11 12:13:14",
    id2: 2,
    name2: "s2",
    time2: "2019-08-11 12:13:14",
};

var mappings = [
    {
        id: "id1",
        name: "name1",
        time: {
            mappingKey: "time1",
            mappingFunc: function (value) {
                return value.substr(0, 10);
            },
            require: true
        }
    },
    {
        id: "id2",
        name: "name2",
        time: {
            mappingKey: "time2",
            mappingFunc: function (value) {
                return value.substr(0, 10);
            },
            require: true
        }
    }
];

console.log(mappingHandle(data, mappings));


